<!--
  * 仓库报表-销售出库汇总
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-card size="small" :bordered="false" :hoverable="false" class="query-form-wrapper smart-margin-bottom10 smart-radius-2">
    <a-form :model="from" labelAlign="left">
      <a-row :gutter="[8, 10]">
        <a-col :span="{ sm: 7, md: 6 }">
          <a-form-item label="单据日期" class="smart-margin-bottom0">
            <custom-range-picker v-model:value="from.create_time" />
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="商品名称" class="smart-margin-bottom0">
            <a-input v-model:value.trim="from.goods_name" placeholder="商品名称、助记码" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="品牌" class="smart-margin-bottom0">
            <Select name="Brand" v-model:value="from.brand_id" placeholder="品牌" />
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="分类" class="smart-margin-bottom0">
            <CateSelect v-model:value="from.cate_id" placeholder="分类" />
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-button-group>
            <a-button type="primary" html-type="submit" @click="onSearch" v-privilege="'goods:query'">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetData" v-privilege="'goods:query'">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
            <SearchFormCollapse v-model="expand" />
          </a-button-group>
        </a-col>
        <template v-if="expand">
          <!-- TODO 需要对接查询字段 -->
          <a-col :span="6">
            <a-form-item label="订单类型" class="smart-margin-bottom0">
              <a-radio-group v-model:value="from.type" @change="onSearch">
                <a-radio-button value="">全部</a-radio-button>
                <a-radio-button value="1">访销</a-radio-button>
                <a-radio-button value="2">车销</a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="订单状态" class="smart-margin-bottom0">
              <a-radio-group v-model:value="from.status" @change="onSearch">
                <a-radio-button value="">全部</a-radio-button>
                <a-radio-button value="1">待审核</a-radio-button>
                <a-radio-button value="2">待出库</a-radio-button>
                <a-radio-button value="3">待签收</a-radio-button>
                <a-radio-button value="4">已完成</a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>
        </template>
      </a-row>
    </a-form>
  </a-card>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true" class="query-table-wrapper">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button @click="onExport" type="primary" v-privilege="'goods:exportGoods'">
          <template #icon>
            <ExportOutlined />
          </template>
          导出
        </a-button>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.ERP.GOODS" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->
    <div class="table-wrapper">
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="goods_id" bordered :scroll="{ x: 'max-content', y: tableScrollY }" :pagination="false" :showSorterTooltip="false" :loading="tableLoading" :row-selection="{ fixed: true, selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }">
        <template #bodyCell="{ text, column, record }">
          <template v-if="column.dataIndex === 'goods_name'">
            <CopyText :text="text">
              {{ text }}
            </CopyText>
          </template>
        </template>
        <template #summary>
          <a-table-summary-row fixed>
            <a-table-summary-cell :index="0">总计</a-table-summary-cell>
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell
              ><a-typography-text type="danger">{{ sumData.total_num }}</a-typography-text></a-table-summary-cell
            >
            <a-table-summary-cell
              ><a-typography-text type="danger">{{ Number(sumData.total_value || 0) }}</a-typography-text></a-table-summary-cell
            >
            <a-table-summary-cell
              ><a-typography-text type="danger">{{ Number(sumData.cost_price || 0).toFixed(2) }}</a-typography-text></a-table-summary-cell
            >
            <a-table-summary-cell :index="columns.length" />
          </a-table-summary-row>
        </template>
      </a-table>
    </div>

    <div class="smart-query-table-page query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="() => `共${total}条`" />
    </div>
  </a-card>
</template>
<script setup lang="ts">
import CustomRangePicker from '@/components/business/custom-range-picker/index.vue'
import { outApi } from '@/api/business/report/warehouse-api'
import TableOperator from '@/components/support/table-operator/index.vue'
import { useQuery } from '@/hooks/web/use-query-hook'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import { TABLE_ID_CONST } from '@/constants/support/table-id-const'
import { useResize } from '@/hooks/web/use-resize-hook'
import Select from '@/components/business/select/index.vue'
import CateSelect from '@/components/business/select/cate-select.vue'
import SearchFormCollapse from '@/components/support/search-form-collapse/index.vue'
// ----------------------------- 计算表格高度 -----------------------------
const { tableScrollY, expand } = useResize()

// ---------------------------- 表格列 ----------------------------

const columns = ref([
  {
    title: '商品名称',
    dataIndex: 'goods_name'
  },
  {
    title: '商品条码',
    dataIndex: 'goods_no'
  },
  {
    title: '商品规格',
    dataIndex: 'spec_name'
  },
  {
    title: '数量',
    dataIndex: 'total_num'
  },
  {
    title: '货值金额',
    dataIndex: 'total_value'
  },
  {
    title: '成本金额',
    dataIndex: 'cost_price'
  },

  {
    title: '调拨时间',
    dataIndex: 'create_time'
  }
])

// ---------------------------- 查询数据表单和方法 ----------------------------
const from = ref({
  create_time: DEFAULT_TIME_RANGE.value,
  goods_name: '',
  brand_id: null,
  outer_warehouse_id: null,
  in_warehouse_id: null,
  cate_id: null,
  type: '',
  status: ''
})
const sumData = ref({
  total_num: 0,
  total_value: 0,
  cost_price: 0
})
const { queryForm, tableData, tableLoading, total, resetQuery, onSearch, queryData } = useQuery({
  fetchDataApi: async (params) => {
    const data = {
      ...params,
      related_type: 2,
      start_time: from.value.create_time ? from.value.create_time[0] : '',
      end_time: from.value.create_time ? from.value.create_time[1] : '',
      goods_name: from.value.goods_name,
      brand_id: from.value.brand_id,
      outer_warehouse_id: from.value.outer_warehouse_id,
      in_warehouse_id: from.value.in_warehouse_id,
      cate_id: from.value.cate_id,
      type: from.value.type,
      status: from.value.status
    }
    const res = await outApi.querySaleList(data)
    sumData.value = res.data.totalSum
    return {
      list: res.data.list.data,
      total: res.data.list.total
    }
  },
  queryFormFilter: {},
  queryFormOp: {}
})
onMounted(() => {
  queryData()
})
// 搜索重置
const resetData = () => {
  from.value.create_time = DEFAULT_TIME_RANGE.value
  from.value.goods_name = ''
  from.value.brand_id = null
  from.value.outer_warehouse_id = null
  from.value.in_warehouse_id = null
  from.value.cate_id = null
  from.value.type = ''
  from.value.status = ''
  queryData()
}

// ---------------------------- 批量 ----------------------------

// 选择表格行
const selectedRowKeyList = ref([])

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys
}
// ------------------------------- 导出和导入 ---------------------------------
// 导出
function onExport() {
  const data = {
    related_type: 2,
    start_time: from.value.create_time ? from.value.create_time[0] : '',
    end_time: from.value.create_time ? from.value.create_time[1] : '',
    goods_name: from.value.goods_name,
    brand_id: from.value.brand_id,
    outer_warehouse_id: from.value.outer_warehouse_id,
    in_warehouse_id: from.value.in_warehouse_id,
    cate_id: from.value.cate_id,
    type: from.value.type,
    status: from.value.status
  }
  outApi.exportOutStatistics(data)
}
</script>
